package com.atguigu.gulimall.order.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000118609970";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDbvjtvMh2gN9aYVsiDgL94MbuOJ1/6nDXfSLd2nv4znBOl4AoiGdsQ5kYrMPkzAJkuHyQYzNVpxqSJnmQiBmBeM+oQNix4MRuwoDwUItGVQaAbgtyHS3u8Yfa2A0u7l3wuvyCoh0jji3aumv6oFS/fNmLmYErw6VvCOWqyeq+bY5Nf/ZSJ/vzGv3isqIbSrx7Cg4IEGUYB/Cissm2XBaQm2ohBwbmyllH0a1r12jnpnjYr8UWBnMJBJoQ/F3LKSnI3QxWSTzkUxatWavUdxWZcW15qLi0BrNk5mgFs8M55cD+bwgRimBd4i1oPGNs2wiJPKLOw6L1drD7VDcfyN8WlAgMBAAECggEAEHkdWYOnRuiItNjkpT8Um30W81wuTMpDG58G6eSXZ+UOcAPwnofTrVul7Pz54YHs+dEp0pYgdZKHqS1vkkFqHgXESTzWbpg33DDW8CeAELFfwFA6GgZlm0S4ywJKPiDoFVUMrLWfmDdPSV2jM5SLBy+7kEutsou77j6BqimaWoqj5GzLR3d56jUCBNra0sGCwd8On+/PzqkXpoaqQ0Ns2tJtvdxq1xilT7RXA0z0vrDvfB8TrDPauFYQKGtlYpsg1qbcwv2hkVBYNL1ubArYs/P7TZmfswLaoRmEfgKYimtfEnBgBKNFbRU5UNrPzMG4MRhHGBISPPg9un1zEO2NQQKBgQDwA1Rbdv6jFh9W8BevKDa3Hal2pSh4tr0C3eluiFpOwdv1G0hTHB8k0hsTeCdn5fMqUcNmyO6Z1hN1F0mbuVF+Puu10HvPRisc1Z7o8TGOFRUJ17fqvXFChKuz4M3f1DBXmkQcTNrSZXJUvA8FTGSlS9eN9vYBgHpZYFryDlcjEQKBgQDqYUNEAMbbWQnK1G/thNT4vbdeHdYwUva0BtXgrs8tQm7V1OtE88NjGSk2yJPQhYja69BmweAvLqRb4ikkIcay89LYZEYfgX2O6mJQkMO+fv5A5cjHuw8pE6cy11+2Q+tCa7bIgfMlEAqrsmMPaV/IoR+i3k9dBx2kfiw3DKURVQKBgQDavh4W0ieW2SWs5Vqgj2T2lPkKodF12w3+W+NQksLDfpkKFVhgLoQP2dFFeD9pyMBKtIQC3GPhHqNTFutGGqq0BYJrjNwZ91h5ihOvN4szc/FdrBX5wbhEqIeYg+z97vQ0Euu6Yy0Xjc+afF6wNISRxcJpp0dj7QUVCeU5MGqoEQKBgQDDmiRvDk7nJGm9sokuqlqqtHnhu/jTH4nwOHwqJPmWBglFv4oRY3Y+r/LrW32w2qCaQ48LEuYrujtEy3wWQXH4SvA904GKLaFBsm4WWpph0s4SDzL/LSe72kEBdyjTjd868XX75CetWLPvEtMFO6XKZF1oqSuGsbeVXg88afsgeQKBgFBWkoMmT6mNXSqsiIu2QYfb3J75JU/9qaKZ/hI03hlxp43Vz6qKPlODk7xo99L0Pen/jLFz+UOM1LzHJUQM0tZ71g9+d4VLa7X6sRwQJshljHH1KR2iFAXtX0kx9lT/1ClT/08fm4fraqTHRKqbFoKMUCgvXwxeLca7W/nSo6Xa";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtX+gwSNYlkR+BovI8bKaWGObMlQ52QG7xWV+qvt7qcaRuFWTqgPow4iefYB6LMsh1GdAW6Unb2qCgN/H0/sAEfUiNtYIQqGc1bySOz4kSxkFf1kQfkJAYpJW6cGz0AQwNq5yRlauT5wGqsUjpmXLwRVG+cX2RvUfFqVzEeSKTa1aW+PwkneUqu/lxTMiXJ8eQf0iB9RaZiWoRszZNcPlVFRbLEugg53/UsSFmoG1mDTs7W39BhNiWgcFKD3SXKSjbuO8zO+erOQBMJ7qqmtmPZ+X+KAlIrGVTGOLe2z4I3oY6pk1wtpFf2+FfnXSlV9CjdRI/YCtbP6uYgtUi6KUQQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url="http://36a86b7693.oicp.vip/payed/orderNotify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url="http://member.gulimall.com/member/toOrderList";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();
        String timeout="2m";

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
